﻿using System;
using System.Linq.Expressions;

namespace DeepEarth.Core.Utilities
{
    public static class BindingUtilities
    {
        public static string PropertyName<T>(Expression<Func<T>> expression)
        {
            return PropertyNameFromLambda(expression);
        }

        public static string PropertyName<TSource, TProperty>(Expression<Func<TSource, TProperty>> expression)
        {
            return PropertyNameFromLambda(expression);
        }

        private static string PropertyNameFromLambda(LambdaExpression expression)
        {
            MemberExpression memberExpression = expression.Body as MemberExpression;

            if (memberExpression != null)
            {
                return memberExpression.Member.Name;
            }
            else
            {
                throw new ArgumentException("Expression body is not a member accessor");
            }
        }
    }
}
